package com.toy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.toy.entity.Product;
import com.toy.mapper.ProductMapper;
import com.toy.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toy.vo.PageResult;
import com.toy.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-08-31
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> findAllByTypeAndProductCategoryId(Integer type, Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        String column = null;
        switch (type){
            case 1:
                column = "categorylevelone_id";
                break;
            case 2:
                column = "categoryleveltwo_id";
                break;
            case 3:
                column = "categorylevelthree_id";
                break;
        }
        queryWrapper.eq(column, id);
        return this.productMapper.selectList(queryWrapper);
    }

    @Override
    public PageResult findItemList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),
                pageResult.getPageSize());
        QueryWrapper<Product> queryWrapper = new QueryWrapper();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"name", pageResult.getQuery());

        page = productMapper.selectPage(page, queryWrapper);
        return pageResult.setTotal(page.getTotal())
                .setRows(page.getRecords());
    }

    @Override
    public void insert(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void deleteItemCat(Product itemCat) {
        productMapper.deleteById(itemCat.getId());
    }

    @Override
    public Product findProductById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updateItemCat(Product itemCat) {
        productMapper.updateById(itemCat);
    }
}
